<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7fc;
            color: #333333;
        }

        .container {
            margin: 0 auto;
            max-width: 500px;
            padding: 40px;
            background-color: #ffffff;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
        }

        h1 {
            font-size: 36px;
            margin-top: 0;
            margin-bottom: 30px;
            text-align: center;
            color: #2196f3;
        }

        form {
            margin-top: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: #666666;
        }

        input[type="text"], input[type="password"], input[type="number"] {
            width: 100%;
            margin-bottom: 20px;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #f4f7fc;
            color: #333333;
            font-size: 16px;
            transition: box-shadow 0.3s ease-in-out;
        }

        input[type="text"]:focus, input[type="password"]:focus, input[type="number"]:focus {
            box-shadow: 0 0 5px #2196f3;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #2196f3;
            color: #ffffff;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease-in-out;
        }

        input[type="submit"]:hover {
            background-color: #007ac1;
        }

        .msg {
            font-size: 14px;
            margin-top: 10px;
            text-align: center;
        }

        .success {
            color: #24af44;
        }

        .error {
            color: #f44336;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>用户注册</h1>
    <form action="/regUser" method="post">
        <label for="username">用户名</label>
        <input id="username" name="username" type="text" required>

        <label for="password">密码</label>
        <input id="password" name="password" type="password" required>

        <label for="nickname">昵称</label>
        <input id="nickname" name="nickname" type="text" required>

        <label for="age">年龄</label>
        <input id="age" name="age" type="number" required>

        <input type="submit" value="注册">

        <div class="msg" id="msg"></div>
    </form>
</div>

<script>
    const form = document.querySelector('form');
    const msg = document.querySelector('#msg');

    form.addEventListener('submit', async (e) => {
        e.preventDefault();
        const { username, password, nickname, age } = form.elements;

        try {
            // 发送注册信息到服务端
            const response = await fetch('/regUser', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username: username.value,
                    password: password.value,
                    nickname: nickname.value,
                    age: age.value,
                }),
            });

            if (response.ok) {
                // 注册成功
                msg.classList.add('success');
                msg.innerText = '注册成功！';
                form.reset();
            } else {
                // 注册失败，显示错误信息
                const json = await response.json();
                msg.classList.add('error');
                msg.innerText = json.message;
            }
        } catch (error) {
            // 请求异常，显示错误信息
            msg.classList.add('error');
            msg.innerText = '网络错误，请重试。';
        }
    });
</script>
</body>
</html>
